
import com.adobe.cairngorm.control.CairngormEventDispatcher;

import flash.events.Event;

import mx.controls.Alert;
import mx.events.CloseEvent;

import net.optix.ip.events.AssignToFirmOtherEvent;
import net.optix.ip.events.GetOrderFirmsEvent;
import net.optix.ip.model.ModelLocator;
import net.optix.ip.vo.InventionVO;

import views.ViewInstance;
[Bindable]
private var model:ModelLocator	=	ModelLocator.getInstance();

private function initApp():void
{
	var getFirms:GetOrderFirmsEvent	=	new GetOrderFirmsEvent();
	com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(getFirms);	
}

private function format(event:Event):void
{
	try
	{
		var money:String	=	usdFormatter.format(event.target.text);
		event.target.text	=	money;
		event.target.selectionBeginIndex	=	money.length;
		event.target.selectionEndIndex		=	money.length;
		trace(event.target.selectionBeginIndex+"   event.target.selectionBeginIndex ");
		trace(event.target.selectionEndIndex+"   event.target.selectionEndIndex ");
		//event.target.replaceText(0,event.target.text.length,usdFormatter.format(event.target.text));
	}
	catch(error:Error)
	{
		trace(error.getStackTrace());
	}
	
}

private function assign():void
{
	if(model.client.bFileOut == false)
	{
		//ViewInstance.showMessage('','Fill out your company information first !');
		clickHandler();
	}
	else
	{
		if(model.currentInvention.type == -1 )
		{
			ViewInstance.showMessage("","Please select either 'Non-Provisional Utility Patent' or 'Provisional Patent' !");
		}
		else
		{
			var vo:InventionVO	=	model.currentInvention;
			vo.firmID			=	firms.selectedItem.firmID;
			vo.maxBudget		=	int(maxBudget.text);
			vo.deadlineDate		=	deadlineDate.text;
			
			var event:AssignToFirmOtherEvent	=	new AssignToFirmOtherEvent(vo);
			com.adobe.cairngorm.control.CairngormEventDispatcher.getInstance().dispatchEvent(event);	
		}
	}
	
	
}

private function clickHandler():void
{
	Alert.yesLabel = "Yes";
 	Alert.show("Fill out your company information first !", "", Alert.OK, this, alertClickHandler);
}

private function alertClickHandler(event:CloseEvent):void 
{
	model.actionView	=	0;
	model.actionView	=	ViewInstance.EditBilling;
}

[Bindable]
private var welcome:String;

private var _invention:InventionVO;

public function set invention(value:InventionVO):void
{
	_invention	=	value;
	if(_invention.firmID == 0)
	{
		welcome	=	"Assign the invention to a law firm without requesting a bid";
	}
	else
	{
		switch(_invention.bConfirm)
		{
			case -1:
				welcome	=	"The firm - "+_invention.firmName+" - decline your assign !";
				break;
			case 0:
				welcome	=	"You have assigned the invention to firm : - "+_invention.firmName+" -";
				break;
			case 1:
				welcome	=	"The firm - : "+_invention.firmName+" - accept your assign !";
				break;
		}
	}
	
}

public function get invention():InventionVO
{
	return _invention;
}